package com.th.supcom.pds.impl.service.internal.config;

import java.util.List;

import net.carefx.framework.exception.BusinessException;
import net.carefx.framework.translate.TranslatorManager;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.th.supcom.pds.domain.pojo.PhamDispensaryConfig;
import com.th.supcom.pds.impl.common.PdsConstants;
import com.th.supcom.pds.impl.manager.PhamDispensaryConfigManager;
import com.th.supcom.pds.internal.api.config.PhamDispensaryConfigService;

public class PhamDispensaryConfigServiceImpl implements PhamDispensaryConfigService
{
    private PhamDispensaryConfigManager m_phamDispensaryConfigManager;
    private TranslatorManager m_translatorManager;

    public void setPhamDispensaryConfigManager (PhamDispensaryConfigManager phamDispensaryConfigManager)
    {
        m_phamDispensaryConfigManager = phamDispensaryConfigManager;
    }

    public void setTranslatorManager (TranslatorManager translatorManager)
    {
        m_translatorManager = translatorManager;
    }

    @Override
    public void addPhamDispensaryConfig (PhamDispensaryConfig phamDispensaryConfig)
    {
        s_logger.info ("调用addPhamDispensaryConfig参数 ：phamDispensaryConfig:{}", new Object[]
        { phamDispensaryConfig });
        if (null == phamDispensaryConfig)
        {
            throw new IllegalArgumentException ("添加药房属性时，保存对象不能为空");
        }
        this.validatePhamDispensaryConfig (phamDispensaryConfig);
        this.validatePhamDispensaryDateRelated (phamDispensaryConfig);
        this.checkPhamDispensaryConfigExistence (phamDispensaryConfig, true);
        try
        {
            m_phamDispensaryConfigManager.addPhamDispensaryConfig (phamDispensaryConfig);
        }
        catch (Exception e)
        {
            s_logger.error ("添加药房属性异常" + e.getLocalizedMessage (), e);
            throw new BusinessException ("添加药房属性异常" + PdsConstants.DEFALUT_HTML_WRAP + e.getLocalizedMessage (), e);
        }
    }

    /**
     * 校验药房属性
     * 
     * @param phamDispensaryConfig
     */
    private void validatePhamDispensaryConfig (PhamDispensaryConfig phamDispensaryConfig)
    {
        if (StringUtils.isEmpty (phamDispensaryConfig.getPhamDispensaryCode ()))
        {
            throw new IllegalArgumentException ("药房编码不能为空");
        }
        if (null == phamDispensaryConfig.getOpenFlag ())
        {
            throw new IllegalArgumentException ("开放标识不能为空");
        }
        if (StringUtils.isEmpty (phamDispensaryConfig.getphamType ()))
        {
            throw new IllegalArgumentException ("药房摆药类型不能为空");
        }
    }

    /**
     * 校验药房时间相关属性
     * 
     * @param phamDispensaryConfig
     */
    private void validatePhamDispensaryDateRelated (PhamDispensaryConfig phamDispensaryConfig)
    {
        if (null == phamDispensaryConfig.getPrescDateBefore ())
        {
            throw new IllegalArgumentException ("处方时间配置不能为空");
        }
        if (StringUtils.isEmpty (phamDispensaryConfig.getShiftType ()))
        {
            throw new IllegalArgumentException ("药房班次类型不能为空");
        }
    }

    @Override
    public void modifyPhamDispensaryConfig (PhamDispensaryConfig phamDispensaryConfig)
    {
        s_logger.info ("调用modifyPhamDispensaryConfig参数 ：phamDispensaryConfig:{}", new Object[]
        { phamDispensaryConfig });
        if (null == phamDispensaryConfig || null == phamDispensaryConfig.getPhamDispensaryConfigId ())
        {
            throw new IllegalArgumentException ("更新药房属性列表时，对象不能为空");
        }
        this.validatePhamDispensaryConfig (phamDispensaryConfig);
        this.validatePhamDispensaryDateRelated (phamDispensaryConfig);
        this.checkPhamDispensaryConfigExistence (phamDispensaryConfig, false);
        try
        {
            m_phamDispensaryConfigManager.modifyPhamDispensaryConfig (phamDispensaryConfig);
        }
        catch (Exception e)
        {
            s_logger.error ("更新药房属性异常" + e.getLocalizedMessage (), e);
            throw new BusinessException ("更新药房属性异常" + PdsConstants.DEFALUT_HTML_WRAP + e.getLocalizedMessage (), e);
        }
    }

    @Override
    public void modifyPhamDispensaryConfigByExample (PhamDispensaryConfig phamDispensaryConfig)
    {
        s_logger.info ("调用modifyPhamDispensaryConfigByExample参数 ：phamDispensaryConfig:{}", new Object[]
        { phamDispensaryConfig });
        if (null == phamDispensaryConfig || null == phamDispensaryConfig.getPhamDispensaryConfigId ())
        {
            throw new IllegalArgumentException ("更新药房属性列表时，对象不能为空");
        }
        this.checkPhamDispensaryConfigExistence (phamDispensaryConfig, false);
        try
        {
            m_phamDispensaryConfigManager.modifyPhamDispensaryConfigByExample (phamDispensaryConfig);
        }
        catch (Exception e)
        {
            s_logger.error ("更新药房属性异常" + e.getLocalizedMessage (), e);
            throw new BusinessException ("更新药房属性异常" + PdsConstants.DEFALUT_HTML_WRAP + e.getLocalizedMessage (), e);
        }
    }

    /**
     * 校验数据库中已有数据
     * 
     * @param phamDispensaryConfig
     * @param isAdd 添加属性时校验，true;更新时校验,false
     */
    private void checkPhamDispensaryConfigExistence (PhamDispensaryConfig phamDispensaryConfig, boolean isAdd)
    {
        PhamDispensaryConfig condition = new PhamDispensaryConfig ();
        condition.setPhamDispensaryCode (phamDispensaryConfig.getPhamDispensaryCode ());
        int count = this.findPhamDispensaryConfigCountByExample (condition);
        // 添加时，校验数据库中是否已存在该药房的属性配置，如果有，抛出异常
        if (isAdd)
        {
            if (count != 0)
            {
                throw new BusinessException ("该药房属性配置已存在");
            }
            return;
        }
        // 更新时，校验数据库中是否已存在该药房的属性配置，如果有，抛出异常
        if (count > 1)
        {
            throw new BusinessException ("该药房属性配置已存在");
        }
    }

    @Override
    public void removePhamDispensaryConfig (PhamDispensaryConfig phamDispensaryConfig)
    {
        s_logger.info ("调用removePhamDispensaryConfig参数 ：phamDispensaryConfig:{}", new Object[]
        { phamDispensaryConfig });
        if (null == phamDispensaryConfig || null == phamDispensaryConfig.getPhamDispensaryConfigId ())
        {
            throw new IllegalArgumentException ("删除药房属性列表时，主键不能为空");
        }
        try
        {
            m_phamDispensaryConfigManager.removePhamDispensaryConfigByID (phamDispensaryConfig.getPhamDispensaryConfigId ());
        }
        catch (Exception e)
        {
            s_logger.error ("通过主键删除药房属性异常" + e.getLocalizedMessage (), e);
            throw new BusinessException ("通过主键删除药房属性异常" + PdsConstants.DEFALUT_HTML_WRAP + e.getLocalizedMessage (), e);
        }
    }

    @Override
    public PhamDispensaryConfig getPhamDispensaryConfigById (Long phamDispensaryConfigId)
    {
        s_logger.info ("调用getPhamDispensaryConfigById参数 ：phamDispensaryConfigId:{}", new Object[]
        { phamDispensaryConfigId });
        if (null == phamDispensaryConfigId)
        {
            throw new IllegalArgumentException ("通过主键查询药房属性时，主键不能为空");
        }
        try
        {
            PhamDispensaryConfig phamDispensaryConfig = m_phamDispensaryConfigManager.getPhamDispensaryConfigByID (phamDispensaryConfigId);
            phamDispensaryConfig = m_translatorManager.translate (phamDispensaryConfig);
            return phamDispensaryConfig;
        }
        catch (Exception e)
        {
            s_logger.error ("通过主键查询药房属性异常" + e.getLocalizedMessage (), e);
            throw new BusinessException ("通过主键查询药房属性异常" + PdsConstants.DEFALUT_HTML_WRAP + e.getLocalizedMessage (), e);
        }
    }

    @Override
    public List <PhamDispensaryConfig> findPhamDispensaryConfigByExample (PhamDispensaryConfig phamDispensaryConfig)
    {
        s_logger.info ("调用findPhamDispensaryConfigByExample参数 ：phamDispensaryConfig:{}", new Object[]
        { phamDispensaryConfig });

        if (null == phamDispensaryConfig)
        {
            throw new IllegalArgumentException ("查询药房属性列表时，查询条件不能为空");
        }
        try
        {
            List <PhamDispensaryConfig> list = m_phamDispensaryConfigManager.findPhamDispensaryConfigListByExample (phamDispensaryConfig);
            list = m_translatorManager.translate (list);
            return list;
        }
        catch (Exception e)
        {
            s_logger.error ("查询药房属性列表异常" + e.getLocalizedMessage (), e);
            throw new BusinessException ("查询药房属性列表异常" + PdsConstants.DEFALUT_HTML_WRAP + e.getLocalizedMessage (), e);
        }
    }

    @Override
    public int findPhamDispensaryConfigCountByExample (PhamDispensaryConfig phamDispensaryConfig)
    {
        s_logger.info ("调用findPhamDispensaryConfigCountByExample参数 ：phamDispensaryConfig:{}", new Object[]
        { phamDispensaryConfig });
        if (null == phamDispensaryConfig)
        {
            throw new IllegalArgumentException ("查询药房属性列表时，查询条件不能为空");
        }
        try
        {
            int count = m_phamDispensaryConfigManager.findPhamDispensaryConfigCountByExample (phamDispensaryConfig);
            return count;
        }
        catch (Exception e)
        {
            s_logger.error ("查询药房属性列表异常" + e.getLocalizedMessage (), e);
            throw new BusinessException ("查询药房属性列表异常" + PdsConstants.DEFALUT_HTML_WRAP + e.getLocalizedMessage (), e);
        }
    }

    @Override
    public PhamDispensaryConfig getPhamDispensaryConfigByPhamDispensaryCode (String phamDispensaryCode)
    {
        if (StringUtils.isBlank (phamDispensaryCode))
        {
            throw new IllegalArgumentException ("通过药房编码查询药房属性时，药房编码不能为空");
        }
        PhamDispensaryConfig phamDispensaryConfig = new PhamDispensaryConfig ();
        phamDispensaryConfig.setPhamDispensaryCode (phamDispensaryCode);
        List <PhamDispensaryConfig> list = this.findPhamDispensaryConfigByExample (phamDispensaryConfig);
        if (list.isEmpty () || list.size () > 1)
        {
            throw new BusinessException ("通过药房代码查询药房属性异常");
        }
        return m_translatorManager.translate (list.get (0));
    }

    /** 日志记录器. */
    public static final Logger s_logger = LoggerFactory.getLogger (PhamDispensaryConfigServiceImpl.class);

    /** 公司版权信息. */
    public static final String copyright = "Copyright 2014, XuNuo Corporation";

    /** 提交路径. */
    public static final String SCC_BRANCH = "$URL$";

    /** 提交日期. */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";

    /** 最终修改人员. */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";

    /** 最终版本号. */
    public static final String SCC_REVISION = "$Revision$";

}
